package com.sxt.pro.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.Screens;
import com.sxt.pro.service.ScreensService;
import com.sxt.pro.vo.ScreensVo;
import com.sxt.sys.common.AppFileUtils;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;


/**  

* @Description: 屏保控制层

* @author xinxiaokang  

* @date 2021年6月28日 下午2:13:39

*/
@RestController
@RequestMapping("/screens")
public class ScreensController {
	
	@Autowired
	private ScreensService screensService;
	
	/**
	 * 查询屏幕分页列表
	 * @param screensVo
	 * @return
	 */
	@RequestMapping("/loadAllScreens")
	public DataGridView loadAllScreens(ScreensVo screensVo){
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 screensVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<Screens> page = new Page<>(screensVo.getPage(), screensVo.getLimit());
		//封装对象查询条件
		QueryWrapper<Screens> queryWrapper = new QueryWrapper<Screens>();
		//筛选条件：网点编号
		  queryWrapper.eq(screensVo.getBanknbr()!=null&&screensVo.getBanknbr()!=0,"banknbr",screensVo.getBanknbr());
		//筛选条件:屏幕名称
		queryWrapper.like(StringUtils.isNotBlank(screensVo.getScreenname()), "personname", screensVo.getScreenname());
		//筛选条件：大于开始时间
		queryWrapper.ge(screensVo.getStartTime()!=null, "createtime", screensVo.getStartTime());
		//筛选条件：小于结束时间
		queryWrapper.le(screensVo.getEndTime()!=null, "createtime", screensVo.getEndTime());
		queryWrapper.orderByDesc("createtime");
		//获取分页数据
		this.screensService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加屏幕
	 * @param screensVo
	 * @return
	 */
	@RequestMapping("addScreens")
	public ResultObj addScreens(ScreensVo screensVo) {
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		screensVo.setBanknbr(banknbr);
		try {
			if(screensVo.getScreenimage()!=null&&screensVo.getScreenimage().endsWith("_temp")) {
				//图片名称
				String newName=AppFileUtils.renameFile(screensVo.getScreenimage());
				screensVo.setScreenimage(newName);
				screensVo.setCreatetime(new Date());
			}
			this.screensService.save(screensVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改屏幕
	 * @param screensVo
	 * @return
	 */
	@RequestMapping("updateScreens")
	public ResultObj updateScreens(ScreensVo screensVo) {
		try {
			//说明是不默认图片
			if(!(screensVo.getScreenimage()!=null&&screensVo.getScreenimage().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(screensVo.getScreenimage().endsWith("_temp")) {
					String newName=AppFileUtils.renameFile(screensVo.getScreenimage());
					screensVo.setScreenimage(newName);
					//删除原先的图片
					String oldPath=this.screensService.getById(screensVo.getId()).getScreenimage();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			this.screensService.updateById(screensVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除屏幕
	 * @param id
	 * @param image
	 * @return
	 */
	@RequestMapping("deleteScreens")
	public ResultObj deleteScreens(Integer id,String screenimage) {
		try {
			//删除原文件
			if (!screenimage.isEmpty() && !screenimage.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(screenimage);
			}
			this.screensService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
}
